package com.monkeygroup.mygoplus;

import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Environment;
import android.support.multidex.MultiDex;

import com.baidu.mapapi.SDKInitializer;
//import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.monkeygroup.mygoplus.ecchat.common.CCPAppManager;
import com.monkeygroup.mygoplus.ecchat.common.utils.CrashHandler;
import com.monkeygroup.mygoplus.ecchat.common.utils.FileAccessor;
import com.monkeygroup.mygoplus.ecchat.ui.contact.entity.ContactMemberDTO;
import com.monkeygroup.mygoplus.http.HttpRequestManager;
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiskCache;
import com.nostra13.universalimageloader.cache.memory.impl.WeakMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.core.download.BaseImageDownloader;

import com.monkeygroup.mygoplus.ecchat.common.utils.ECPreferenceSettings;
import com.monkeygroup.mygoplus.ecchat.common.utils.ECPreferences;
import com.monkeygroup.mygoplus.ecchat.common.utils.LogUtil;
import com.monkeygroup.mygoplus.ecchat.ui.huawei.PustDemoActivity;
import com.nostra13.universalimageloader.utils.StorageUtils;

import java.io.File;
import java.io.InvalidClassException;
import java.util.List;
import java.util.Map;

public class MyGoApplication extends Application {
    private static Context applicationContext;
    private static MyGoApplication instance;

    private static PustDemoActivity mPustTestActivity = null;

    /**
     * 把好友列表存全局，以便分享好友
     **/
    private static Map<String, ContactMemberDTO> contacts;

    /**
     * 把封锁好友存全局，以便用来判断
     **/
    private static Map<String, ContactMemberDTO> blockList;

    /**
     * 把關心列表存全局，以便用来判断
     **/
    private static Map<String, ContactMemberDTO> concernList;

    /**
     * 好友請求個數臨時保存
     **/
    public static int applyNum = 0;
    /***
     * 屏幕宽高保存
     **/
    public static int fullScreenWidth = 0;
    public static int fullScreenHeight = 0;
    /***
     * 保存软键盘的高度
     **/
    public static int softKeyboardHeight = 0;
    /***
     * 网络请求token保存
     **/
    public static String token = "";
    /***
     * 信号栏高度保存
     **/
    public static int statusHight = 0;
    /***
     * 容联云userid
     **/
    public static String myECUserid = "";
    /***
     * 昵称
     **/
    public static String myNickname = "";
    /***
     * 應用登錄狀態
     **/
    public static boolean loginFlag = false;
    private static final String TAG = "MyGoApplication";

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        applicationContext = this.getApplicationContext();

        init();
    }

    /**
     * 单例，返回一个实例
     *
     * @return
     */
    public static MyGoApplication getInstance() {
        if (instance == null) {
            LogUtil.w("[MyGoApplication] instance is null.");
        }
        return instance;
    }

    private void init() {
        CCPAppManager.setContext(instance);
        FileAccessor.initFileAccess();
        setChattingContactId();
        CrashHandler.getInstance().init(this);
        initImageLoaderConfig(applicationContext);
        HttpRequestManager.initRequestQueue(this);
        Fresco.initialize(this);
        SDKInitializer.initialize(this);
    }

    public static String getBaseDir() {
        boolean sdCardExist = Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);

        if (sdCardExist) {
            return Environment.getExternalStorageDirectory() + "/mygo";
        }

        return null;
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }

    /**
     * 保存当前的聊天界面所对应的联系人、方便来消息屏蔽通知
     */
    private void setChattingContactId() {
        try {
            ECPreferences.savePreference(ECPreferenceSettings.SETTING_CHATTING_CONTACTID, "", true);
        } catch (InvalidClassException e) {
            e.printStackTrace();
        }
    }

    public void setMainActivity(PustDemoActivity activity) {
        mPustTestActivity = activity;
    }

    public PustDemoActivity getMainActivity() {
        return mPustTestActivity;
    }

    private void initImageLoaderConfig(Context mContext) {
        File cacheDir = StorageUtils.getOwnCacheDirectory(getApplicationContext(), "mygo/image");
        ImageLoaderConfiguration config = new ImageLoaderConfiguration
                .Builder(mContext)
                .memoryCache(new WeakMemoryCache())
                .memoryCacheSize(2 * 1024 * 1024)
                .threadPoolSize(3)
                .threadPriority(Thread.NORM_PRIORITY - 2)
                .diskCacheFileNameGenerator(CCPAppManager.md5FileNameGenerator)
                .diskCache(new UnlimitedDiskCache(cacheDir, null, CCPAppManager.md5FileNameGenerator))//自定义缓存路径
                .tasksProcessingOrder(QueueProcessingType.LIFO)
                .defaultDisplayImageOptions(DisplayImageOptions.createSimple())
                .imageDownloader(new BaseImageDownloader(mContext, 5 * 1000, 30 * 1000))
                .writeDebugLogs().build();

        ImageLoader.getInstance().init(config);
    }

    public static DisplayImageOptions getDefaultDisplayImgOption() {
        DisplayImageOptions options = new DisplayImageOptions.Builder()
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .bitmapConfig(Bitmap.Config.RGB_565).build();

        return options;
    }

    private String getRunningActivityName() {
        ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
        String runningActivity = activityManager.getRunningTasks(1).get(0).topActivity.getClassName();

        return runningActivity;
    }

    public boolean isCurrActivity(Class<?> cl) {
        if (getRunningActivityName().equals(cl.getName())) {
            return true;
        }

        return false;
    }

    public static Map<String, ContactMemberDTO> getContacts() {
        return contacts;
    }

    public static void setContacts(Map<String, ContactMemberDTO> contacts) {
        MyGoApplication.contacts = contacts;
    }

    public static Map<String, ContactMemberDTO> getBlockList() {
        return blockList;
    }

    public static void setBlockList(Map<String, ContactMemberDTO> blockList) {
        MyGoApplication.blockList = blockList;
    }

    public static Map<String, ContactMemberDTO> getConcernList() {
        return concernList;
    }

    public static void setConcernList(Map<String, ContactMemberDTO> concernList) {
        MyGoApplication.concernList = concernList;
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
    }
}
